package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CustomerBOInterface;
import org.cuatrovientos.erp.business.StockBOInterface;
import org.cuatrovientos.erp.model.Stock;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Customer management
 * 
 * @author VB
 */
@Controller
public class StockController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(StockController.class);

	@Autowired
	private StockBOInterface stockBO;

	/**
	 * default constructor
	 */
	public StockController() {
		logger.info("stock BEAN instantiated");
	}

	/**
	 * handles default /stock or /stock/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/customer","/customer/list" )
	 */
	@RequestMapping({ "/stock", "/stock/list" })
	public ModelAndView stockDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Stock> stockList = stockBO.selectAll();
		model.put("stock", stockList);
		return new ModelAndView("stockList", model);
	}

	/**
	 * handles /stock/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/stock/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/stock/search" })
	public ModelAndView search(@RequestParam(value = "description", defaultValue = "", required = true) String description) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Stock> stockList = stockBO.search(description);
		model.put("stock", stockList);

		return new ModelAndView("stockList", model);
	}

	/**
	 * handles delete stock, and redirect to stock default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/stock","/stock/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/stock/delete" })
	public String deletestock(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		stockBO.deleteById(id);
		return "redirect:/stock";
	}

	/**
	 * handles new stock request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "stock"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/stock/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/stock/new" })
	public ModelAndView newstock() {
		logger.info("new stock ");
		Stock stock = new Stock();

		return new ModelAndView("Newstock", "stock", stock);
	}

	/**
	 * handles new stock request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/stock/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/stock/save" })
	public String newstock(@Valid Stock stock,BindingResult bindingResult) {
		logger.info("new stock " + stock.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("stock", stock);
			return "Newstock";
		}

		// If data is ok, insert and go on
		stockBO.insert(stock);
		return "redirect:/stock";
	}

	/**
	 * handles delete stock, and redirect to stock default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/stock","/stock/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/stock/detail" })
	public ModelAndView customerDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Stock stock = stockBO.selectById(id);
		model.put("stock", stock);

		return new ModelAndView("stockDetail", model);
	}

	/**
	 * handles stock update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/stock/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/stock/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Stock stock = stockBO.selectById(id);
		model.put("stock", stock);
		return new ModelAndView("Updatestock", model);

	}

	/**
	 * handles save update stock request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/stock/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/stock/saveupdate" })
	public String saveUpdate(@Valid Stock stock,BindingResult bindingResult) {
		logger.info("update stock " + stock.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("stock", stock);
			return "Updatestock";
		}

		stockBO.update(stock);
		return "redirect:/stock";
	}
}
